<template>
  <div>
    <el-dialog title="新增机构" :visible.sync="showAddStoreDialog" center width="770px">
      <el-form :model="organizationData" :rules="rules" ref="organizationData">

        <el-form-item label="机构名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="organizationData.name" autocomplete="off"></el-input>
        </el-form-item>


        <el-form-item label="所属用户" :label-width="formLabelWidth">
          <el-select v-model="organizationData.customer_id" filterable placeholder="请选择">
            <el-option
              v-for="item in customerList"
              :key="item.customer_id"
              :label="item.nickname"
              :value="item.customer_id">
            </el-option>
          </el-select>
        </el-form-item>



        <el-form-item label="所属省份和城市" :label-width="formLabelWidth">
          <el-cascader placeholder="试试搜索：广东"
                       :options="provinceAndCityList"
                       :props="{ value: 'id', label: 'label'}"
                       @change="handleChange"
                       filterable>
          </el-cascader>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-radio v-model="organizationData.status" :label="0">停用</el-radio>
          <el-radio v-model="organizationData.status" :label="1">启用</el-radio>
        </el-form-item>


        <el-form-item label="详细地址" :label-width="formLabelWidth">
          <el-input v-model="organizationData.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系人" :label-width="formLabelWidth">
          <el-input v-model="organizationData.contact" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" :label-width="formLabelWidth" prop="telephone">
          <el-input v-model="organizationData.telephone" autocomplete="off"></el-input>
        </el-form-item>




      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelSubmit">取 消</el-button>
        <el-button type="primary" @click="submitStore('organizationData')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import store from '@/store'
  import {add} from '@/api/thk/organization'
  import {dateFormat} from '@/utils/dateFormat'

  export default {
    name: 'addOrganization',

    data() {
      return {

        /**
         customer_organization_id       id(不用传，后台自动生成)
         customer_id                    用户id
         name                           机构名称
         city                           所属城市
         province                       所属城市
         address                        详细地址
         status                         状态
         contact                        联系人
         telephone                      联系电话
         date_added                     添加日期
         date_modified                  最后修改时间
         */

        showAddStoreDialog: false,
        formLabelWidth: '130px',
        organizationData: {
          customer_id: '',
          name: '',
          city: '',
          province: '',
          address: '',
          status: 1,                        // 状态默认启用
          contact: '',
          telephone: '',
          date_added: '',
          date_modified: '',
          code: '1234'
        },

        customerList: [],
        provinceAndCityList: [],

        // 表单校验规则
        rules: {
          // 店铺名为必须
          name:[
            { required: true, message: '请输入机构名称', trigger: 'blur' },
          ],
          telephone:[
            { required: true, message: '请输手机号', trigger: 'blur' },
            { min: 11, max: 11, message: '长度必须为11位', trigger: 'blur' }
          ]
        }
      }
    },



    methods: {
      // 判断是否点击了提交
      submitStore(organizationData){
        // 设置添加的时间
        this.organizationData.date_added = dateFormat("YYYY-mm-dd HH:MM:SS", new Date())
        this.$refs[organizationData].validate((valid) => {
          if (valid) {

            console.log('---------- organizationData --------->')
            console.log(this.organizationData)

            add(store.getters.token,this.organizationData).then(result => {
              // 增加成功
              this.showAddStoreDialog = !this.showAddStoreDialog
              this.$parent.getOrganizationList()
            }).catch(error => {
              console.log(error)
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });

      },

      cancelSubmit(customerList,provinceAndCityList){
        this.customerList = customerList
        this.provinceAndCityList = provinceAndCityList
        this.showAddStoreDialog = !this.showAddStoreDialog
      },

      handleChange(value){
        if(value){
          this.organizationData.province = value[0]
          this.organizationData.city = value[1]
        }
      }

    }
  }
</script>
